package inference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Graph {
	private HashMap<String, GraphEdge> _edges;
	private HashMap<String, GraphNode> _nodes;

	// ArrayList<GraphNode> visited = new ArrayList<GraphNode>();

	// public ArrayList<GraphNode> edges() {
	// return visited;
	// }

	public void collectEdges() {
		_edges = new HashMap<String, GraphEdge>();

		for (GraphNode n : queryNodes()) {
			collectEdges(n);
		}
	}

	private void collectEdges(GraphNode n) {
		if (_edges.containsValue(n))
			return;
		for (String s : n.edges().keySet()) {
			for (GraphNode n1 : n.edges().get(s).nodes()) {
				collectEdges(n1);
			}
		}
	}

	public void collectNodes() {
		_nodes = new HashMap<String, GraphNode>();
		for (GraphNode n : queryNodes()) {
			collectNodes(n);
		}
	}

	public HashMap<String, GraphEdge> edges() {
		return _edges;
	}

	public GraphNode node(String name) {
		return _nodes.get(name);
	}

	private void collectNodes(GraphNode n) {
		if (_nodes.containsValue(n))
			return;
		_nodes.put(n.name, n);
		for (String s : n.edges().keySet()) {
			for (GraphNode n1 : n.edges().get(s).nodes()) {
				collectNodes(n1);
			}
		}
	}

	private List<GraphNode> _evidenceNodes = new ArrayList<GraphNode>();

	public List<GraphNode> evidenceNodes() {
		return _evidenceNodes;
	}

	private List<GraphNode> _queryNodes = new ArrayList<GraphNode>();

	public List<GraphNode> queryNodes() {
		return _queryNodes;
	}

}
